package cn.lopr.im.android.mvp.presenter;

import android.util.Log;

import com.google.gson.Gson;

import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.db.entity.Message;
import cn.lopr.im.android.db.helper.DaoUtilsStore;
import cn.lopr.im.android.mvp.contact.IChatContact;
import cn.lopr.im.android.mvp.view.activities.ChatActivity;
import cn.lopr.im.android.mvp.view.activities.MainActivity;
import cn.lopr.im.android.utils.DateUtil;
import cn.lopr.im.android.utils.IMWebSocketClient;
import cn.lopr.im.android.utils.ParameterUtil;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class ChatPresenterImpl implements IChatContact.IChatPresenter {

    private static final String TAG = "ss";
    private final Activity activity;
    private final IChatContact.IChatView chatView;

    public ChatPresenterImpl(ChatActivity chatActivity, IChatContact.IChatView chatView) {
        this.chatView = chatView;
        this.activity = chatActivity;
    }

    @Override
    public void sendMessage() {
        String content = this.chatView.getContent();
        if (ParameterUtil.checkParameter(content)) {
            return;
        }

        Message message = new Message().setFromUser(MainActivity.user.getPhone())
                .setToUser(this.chatView.getFriend().getPhone())
                .setType(Constant.MessageType.SEND_TO_USER.getCode())
                .setDate(DateUtil.getTime()).setGroupId(null)
                .setMsg(content);
        Log.i(TAG, "sendMessage: " + DateUtil.getTime());
        this.chatView.addMessage(message);
        this.chatView.setContent(null);

        String json = new Gson().toJson(message);
        IMWebSocketClient.getWebSocketClient().send(json);

        this.chatView.closeInput();
//        binding.emojiGrid.setVisibility(View.GONE);

        DaoUtilsStore.getInstance().getMessageDaoUtils().insert(message.setIsRead(1));

    }
}
